﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XmlXmlToEditItemWorker
	{
		
		public static int parseItem(XmlToEditContext context)
		{
			XmlToEdit xmlToEdit = context.XmlToEdit;
			string item = xmlToEdit.Item;
			if (item == null || item.Length <= 0)
			{
				throw new InfoJetException("no item", "XXEI01");
			}
			
			XmlElement root = context.Doc.DocumentElement;
			XmlNodeList itemXmlNodeList = DOMUtil.selectNodes(root, item, 0, context.NsResolver);
			if (itemXmlNodeList.Count <= 0)
			{
				throw new InfoJetException("no item", "XXEI02");
			}
			
			ArrayList contextIdList = context.ContextIdList;
			XmlNode itemNode = XmlToEditUtil.findNodeByContextId(itemXmlNodeList, contextIdList);
			if (itemNode == null)
			{
				throw new InfoJetException("item not in context", "XXEI03");
			}
			else
			{
				context.ItemNode = itemNode;
			}
			
			return itemXmlNodeList.Count;
		}
	}
}